package com.jzr.bedside.db1.vo;

import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Getter
@Setter
public class MyUserDetailsVo extends User {
    private Integer userId;
    private String token;

    public MyUserDetailsVo(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public MyUserDetailsVo(Integer userId, String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.userId = userId;
    }

    public MyUserDetailsVo(Integer userId, String username, String password, String token, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.userId = userId;
        this.token = token;
    }

    public String Authorities2ListStr(){
        String s = "";
        for(GrantedAuthority authority :this.getAuthorities()){
            String r = ((SimpleGrantedAuthority)authority).toString();
            if(s != null) {
                if (s.length() > 0) {
                    s = s.concat(",").concat(r);
                } else {
                    s = s.concat(r);
                }
            }
        }
        return s;
    }
    public static List<GrantedAuthority> GetAuthoritiesFromStr(String authoritiesString){
        List<GrantedAuthority> authorities = new ArrayList<>();

        if(authoritiesString != null && authoritiesString.length() > 0){
            String[] list = authoritiesString.split(",");
            for (String s : list) {
                authorities.add(new SimpleGrantedAuthority(s));
            }
        }
        return authorities;
    }
}
